package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BERol;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DARol extends DAOAbstract<BERol> {

    public DARol(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BERol be) throws Exception {

        try {
            setStoreProcedure("zapateria.sp_ins_rol");

            setParameterString(be.getNombre());
            setParameterInt(be.getPersonaid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BERol be) throws Exception {

        try {
            setStoreProcedure("zapateria.sp_upd_rol");
            setParameterInt(be.getRolid());
            setParameterString(be.getNombre());
            setParameterInt(be.getPersonaid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }

    }

    @Override
    public int eliminarRegistro(BERol be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_rol");
            setParameterInt(be.getRolid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BERol> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_rol");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BERol be = new BERol();
                be.setRolid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setPersonaid(getDataInt(3));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BERol> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_rol");
            setParameterString("");
            commitc();
            while (existResult()) {
                BERol be = new BERol();
                be.setRolid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setPersonaid(getDataInt(3));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }

    }

    @Override
    public BERol buscar(int i) throws Exception {
        try {
            BERol be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_rol_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BERol();
                be.setRolid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setPersonaid(getDataInt(3));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public void eliminar(int id) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_rol");
            setParameterInt(id);
            commit();
        } catch (Exception e) {
            rollback(e);
        }
    }

    public ArrayList<BERol> listar_modulos_usuario(int id) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_rol");
            setParameterInt(id);
            commitc();
            while (existResult()) {
                BERol be = new BERol();
                be.setRolid(getDataInt(1));
                be.setNombre(getDataString(2));
                be.setPersonaid(getDataInt(3));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }
}
